/* SDLWindow.h
 *
 * --
 * Copyright (c) 2012, Christophe Huriaux
 * Distributed under the terms of the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * --
 */

#ifndef SDL_WINDOW_H
#define SDL_WINDOW_H

#include <bitset>
#include <string>
#include <SDL/SDL.h>
#include <thread/thread.h>

class SDLWindow : public Thread
{
	unsigned int windowWidth;
	unsigned int windowHeight;
	unsigned int windowBpp;
	std::string windowTitle;
	bool fullscreen;

	int videoModeAdditionalFlags;

	SDL_Surface* screen;

	//
	bool inputGrabbed;
	std::bitset<SDLK_LAST> keys;
	int lastMotionX;
	int lastMotionY;
	bool mouseMoved;

public:
	SDLWindow(const char* title, unsigned int width, unsigned int height,
			unsigned int bpp, bool fullscreen);
	SDLWindow(const char* title, unsigned int width, unsigned int height,
			unsigned int bpp);
	SDLWindow(const char* title, unsigned int width, unsigned int height);
	virtual ~SDLWindow();

	virtual int open();

	unsigned int width() { return windowWidth; }
	unsigned int height() { return windowHeight; }
	unsigned int getBpp() { return windowBpp; }
	std::string& getTitle() { return windowTitle; }
	bool isFullscreen() { return fullscreen; }

	int setWidth(unsigned int width);
	int setHeight(unsigned int height);
	int setBpp(unsigned int bpp);
	void setTitle(std::string& title);
	int setFullscreen(bool fullscreen);

	std::bitset<SDLK_LAST>& keyState() { return keys; }
	bool hasMouseMoved() { bool m = mouseMoved; mouseMoved = false; return m; }
	int mouseMoveX() { return lastMotionX; }
	int mouseMoveY() { return lastMotionY; }

protected:
	void* run();
	virtual void atLaunch() {};
	virtual void atCleanup();
	virtual void callbackLoop() { };

	virtual int init();
	virtual int setVideoMode();
	virtual int updateVideoMode(int flags);
	virtual int updateVideoMode();
};
#endif // SDL_WINDOW_H
